﻿$(document).ready(function () {
    //element cache
    var J_mobile = $("#J_mobile");
    var J_creditcard = $("#J_creditcard");
    var J_movie_city = $("#J_movie_city");
    var J_movie_cinema = $("#J_movie_cinema");
    var J_movie_film = $("#J_movie_film");
    var J_recent_buy = $("#J_recent_buy");
    //tab
    tab($("#J_news"));
    tab($("#J_handy"));
    tab($("#J_game"));
    tab(J_recent_buy);
    //lazyload
    setTimeout("$(\"img[data-lazyload]:not(#J_slide_billboard img[data-lazyload])\").lazyload({effect: \"fadeIn\",threshold: 5,failurelimit: 5});", 1000);
    $("#J_slide_billboard img[data-lazyload]").lazyload({ effect: "fadeIn", threshold: 5, failurelimit: 5 });
    //小目录
    $("#J_guide").hover(function () {
        $(this).stop().addClass("guide-all").animate({ height: "319px" }, 500);
    }, function () {
        $(this).stop().animate({ height: "235px" }, 500, function () { $(this).removeClass("guide-all") });
    });
    //最近购买滚动
    if ($("#J_lastbuy").find("a").length < 1) {
        J_recent_buy.find(".tab-holder").find("li").eq(1).mouseover();
    } else {
        setInterval("slide($('#J_lastbuy'),24,500)", 1000);
    };
    //电影
    tab($("#J_movie"));
    //公告滚动
    var notice = $("#J_notice");
    var notice_html = notice.html();
    if (notice_html.length > 36) {
        notice.html("<marquee direction='left' scrollamount='2' scrolldelay='1' onmouseover='this.stop()' onmouseover='this.stop()' onmouseout='this.start()'>" + notice_html + "</marquee>");
    };
    //宣传广告轮播
    var J_billboard = $("#J_slide_billboard");
    J_billboard.removeClass("loading").find(".slide-list").show();
    J_billboard.find(".slide-triggers li").mouseover(function (event) {
        event.stopPropagation();
        var eq = $.inArray(this, $.makeArray(J_billboard.find(".slide-triggers li")));
        var count = J_billboard.find(".slide-triggers li").length;
        J_billboard.attr("data-eq", eq).find(".slide-list").animate({ top: -eq * 200 }, 500).children().removeAttr("style");
        $(this).parent().find("li").removeClass("slide-current").eq(eq).addClass("slide-current");
    }).eq(0).mouseover();
    //热点轮播
    var J_promo = $("#J_promo");
    var J_promo_ul = J_promo.find(".slide-list");
    var J_promo_ul_li = J_promo_ul.find("li");
    var steps = 217;
    J_promo_ul.css({ width: J_promo_ul_li.length * steps });
    var step = J_promo_ul_li.length - 3;
    $("#J_promo_up").attr("href", "javascript:void(0)").click(function () {
        var left = parseInt(J_promo_ul.css("left"));
        var eq = parseInt(J_promo.attr("data-eq"));
        eq += -1;
        J_promo_ul_li.eq(eq).removeAttr("style");
        J_promo.attr("data-eq", eq);
        if (left < 0) {
            left += steps;
        };
        J_promo_ul.animate({ left: left }, 500);
    });
    $("#J_promo_down").attr("href", "javascript:void(0)").click(function () {
        roll($("#J_promo"), 217, 500);
    });
    //便民
    //手机冲值
    J_mobile.submit(function () {
        var t = $(this);
        var input = t.find("input");
        var reg = /^(((13[0-9]{1})|(147)|(15[0-35-9]{1})|(18[025-9]{1}))+\d{8})$/;
        var i0 = input.eq(0).val().replace(/(^\s*)|(\s*$)/g, "");
        var i1 = input.eq(1).val().replace(/(^\s*)|(\s*$)/g, "");
        if (!reg.exec(i0)) { alert("请填写正确的手机号码"); return false; };
        if (i1 != i0) { alert("请确认两次输入手机号码是否相同"); return false; };
    });
    J_mobile.find("input[type='text']").keyup(function () {
        var t = $(this);
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        var reg = /[^\d]$/;
        if (val.length > 0) {
            val = val.replace(reg, "");
            val = val.substring(0, 11);
            t.val(val);
        };
    });
    //电影票
    J_movie_cinema.parent().next(".handy-btn").find("a").attr("href", "javascript:alert('请选择城市');");
    var movie_city_html = "";
    for (var i = 0; i < _handy_movies.length; i++) {
        movie_city_html += _handy_movies[i].parentId == 0 ? "<p data='" + _handy_movies[i].id + "'>" + _handy_movies[i].name + "</p>" : "";
    };
    J_movie_city.find(".handy-selectcontent").html(movie_city_html);
    J_movie_city.find("input").val("请选择城市");
    J_movie_film.find("input").val("请选择电影");
    J_movie_cinema.find("input").val("请选择影院");
    J_movie_city.click(function () {
        var t = $(this).find("input");
        J_movie_city.find(".handy-selectcontent").show().css({ left: t.offset().left - 1, top: t.offset().top + t.height() - 1 }).focus();
        J_movie_city.find(".handy-selectcontent").blur(function () {
            $(this).hide();
        });
        J_movie_film.find(".handy-selectcontent").hide();
        J_movie_cinema.find(".handy-selectcontent").hide();
    });
    J_movie_city.find(".handy-selectcontent p").live("click", function () {
        J_movie_city.find("input").val($(this).html()).attr("data", $(this).attr("data"));
        J_movie_city.find(".handy-selectcontent").hide();
        J_movie_cinema.find(".handy-selectcontent").html("");
        //刷新影片
        var url = J_movie_film.find("input").attr("data-load") + "?r=" + Math.random() + "&city=" + $(this).attr("data");
        J_movie_film.find("input").val("请选择电影");
        J_movie_cinema.find("input").val("请选择影院");
        $.getJSON(url, function (data) {
            var html = "";
            for (var i = 0; i < data.length; i++) {
                html += "<p data='" + data[i].id + "'>" + data[i].name + "</p>";
            };
            J_movie_film.find(".handy-selectcontent").html(html);
        });
        J_movie_cinema.parent().next(".handy-btn").find("a").attr("href", "javascript:alert('请选择电影');").removeAttr("target");
    });
    J_movie_film.click(function () {
        var t = $(this).find("input");
        J_movie_film.find(".handy-selectcontent").show().css({ left: t.offset().left - 1, top: t.offset().top + t.height() - 1 }).focus();
        J_movie_film.find(".handy-selectcontent").blur(function () {
            $(this).hide();
        });
        J_movie_cinema.find(".handy-selectcontent").hide();
    });
    J_movie_film.find(".handy-selectcontent p").live("click", function () {
        J_movie_film.find("input").val($(this).html()).attr("data", $(this).attr("data"));
        J_movie_film.find(".handy-selectcontent").hide();
        //刷新影片
        var url = J_movie_cinema.find("input").attr("data-load") + "?r=" + Math.random() + "&city=" + J_movie_city.find("input").attr("data") + "&film=" + $(this).attr("data");
        J_movie_cinema.find("input").val("请选择影院");
        $.getJSON(url, function (data) {
            var html = "";
            for (var i = 0; i < data.length; i++) {
                html += "<p data='" + data[i].id + "'>" + data[i].name + "</p>";
            };
            J_movie_cinema.find(".handy-selectcontent").html(html);
        });
        //超链接
        var href = J_movie_cinema.parent().next(".handy-btn").find("a");
        var url = href.attr("data-href");
        url = url.replace("{city}", J_movie_city.find("input").attr("data"));
        url = url.replace("{film}", J_movie_film.find("input").attr("data"));
        url = url.replace("&cinema={cinema}", "");
        href.attr("href", url).attr("target", "_blank");
    });
    J_movie_cinema.click(function () {
        var t = $(this).find("input");
        J_movie_cinema.find(".handy-selectcontent").show().css({ left: t.offset().left - 1, top: t.offset().top + t.height() - 1 }).focus();
        J_movie_cinema.find(".handy-selectcontent").blur(function () {
            $(this).hide();
        });
    });
    J_movie_cinema.find(".handy-selectcontent p").live("click", function () {
        J_movie_cinema.find("input").val($(this).html()).attr("data", $(this).attr("data"));
        J_movie_cinema.find(".handy-selectcontent").hide();
        //超链接
        var href = J_movie_cinema.parent().next(".handy-btn").find("a");
        var url = href.attr("data-href");
        url = url.replace("{city}", J_movie_city.find("input").attr("data"));
        url = url.replace("{film}", J_movie_film.find("input").attr("data"));
        url = url.replace("{cinema}", J_movie_cinema.find("input").attr("data"));
        href.attr("href", url).attr("target", "_blank");
    });

    //信用卡
    var cardhtml = "";
    for (var i = 0; i < _handy_creditcards.length; i++) {
        cardhtml += "<p data='" + _handy_creditcards[i].bankRedirectURL + "'>" + _handy_creditcards[i].bankName + "</p>";
    };
    J_creditcard.find(".handy-selectcontent").html(cardhtml);
    J_creditcard.click(function () {
        var t = $(this).find("input");
        J_creditcard.find(".handy-selectcontent").show().css({ left: t.offset().left - 1, top: t.offset().top + t.height() - 1 }).focus();
        J_creditcard.find(".handy-selectcontent").blur(function () {
            $(this).hide();
        });
    });
    J_creditcard.find(".handy-selectcontent p").live("click", function () {
        J_creditcard.find("input").val($(this).html());
        J_creditcard.find(".handy-selectcontent").hide();
        J_creditcard.parent().next(".handy-btn").find(".handy-abtn").attr("href", $(this).attr("data")).attr("target", "_blank");
    });
    J_creditcard.parent().next(".handy-btn").find(".handy-abtn").attr("href", "javascript:void(0)");
    //提示关闭
    $("#J_content_tip").find("button").click(function () {
        $("#J_content_tip").find("p").fadeOut("1000");
        $("#J_content_tip").slideUp("1000");
    });
});

//轮播
function slide(element, step, time) {
    var slide_list = element.find(".slide-list");
    var slide_triggers = element.find(".slide-triggers");
    var count = slide_list.children().length;
    if ((count < 2) || (slide_list.height() < element.height() + step)) { return; }
    var slide_list_first = slide_list.children().eq(0);
    var eq = parseInt(element.attr("data-eq"));
    if (isNaN(eq)) { eq = -1; element.attr("data-eq", "0"); };
    eq += 1;
    if (eq > count - 1) {
        eq = 0;
        slide_list.animate({ top: -count * step }, time, function () {
            slide_list.animate({ top: 0 }, 0);
            slide_list.children().removeAttr("style");
        });
    } else {
        slide_list.animate({ top: -eq * step }, time);
    }
    if (slide_list.height() - count * step < element.height()) {
        for (var i = 0; i < eq - 1; i++) {
            slide_list.children().eq(i).css({ "position": "relative", "top": (count * step) + "px" });
        };
    };
    element.attr("data-eq", eq);
    slide_triggers.children().removeClass("slide-current").eq(eq).addClass("slide-current");
};

//滚动
function roll(element, step, time) {
    var slide_list = element.find(".slide-list");
    var count = slide_list.children().length;
    if ((count < 2) || (slide_list.width() < element.width() + step)) { return; }
    var slide_list_first = slide_list.children().eq(0);
    var eq = parseInt(element.attr("data-eq"));
    if (isNaN(eq)) { eq = -1; element.attr("data-eq", "0"); };
    eq += 1;
    if (eq > count - 1) {
        eq = 0;
        slide_list.animate({ left: -count * step }, time, function () {
            slide_list.animate({left: 0 }, 0);
            slide_list.children().removeAttr("style");
        });
    } else {
        slide_list.animate({ left: -eq * step }, time);
    }
    if (slide_list.width() - count * step < element.width()) {
        for (var i = 0; i < eq - 1; i++) {
            slide_list.children().eq(i).css({ "position": "relative", "left": (count * step) + "px" });
        };
    };
    element.attr("data-eq", eq);
}

//tab
function tab(element) {
    element.find(".tab-holder").children().mouseover(function (event) {
        event.stopPropagation();
        $(this).parent().find(".tab-current").removeClass("tab-current");
        $(this).addClass("tab-current");
        var eq = $.inArray(this, $.makeArray(element.find(".tab-holder").children()));
        element.find(".tab-panel").children().not(element.find(".tab-panel").children().eq(eq)).hide();
        element.find(".tab-panel").children().eq(eq).show();
    }).eq(0).mouseover().mouseout(function (event) { event.stopPropagation(); }).mouseout();
    var href = element.find(".tab-holder a");
    if (href.attr("href") == "" || href.attr("href") == "#" || href.attr("href") == null) { href.attr("href", "javascript:void(0)"); }
};

//广告
var billboard_play = setInterval("slide($('#J_slide_billboard'),200,500)", 4000);
$("#J_slide_billboard").hover(function () {
    if (!!window.billboard_play) {
        billboard_play = clearInterval(billboard_play);
    };
}, function () {
    if (!!!window.billboard_play) {
        billboard_play = setInterval("slide($('#J_slide_billboard'),200,500)", 4000);
    };
});
$("#J_slide_billboard").find("a").hover(function () {
    if (!!window.billboard_play) {
        billboard_play = clearInterval(billboard_play);
    };
}, function () {
    if (!!!window.billboard_play) {
        billboard_play = setInterval("slide($('#J_slide_billboard'),200,500)", 4000);
    };
});

//热点左右滚动
var J_promo = $("#J_promo");
var promo_play = setInterval("roll($('#J_promo'),217,500)", 4000);
J_promo.hover(function () {
    if (!!window.promo_play) {
        promo_play = clearInterval(promo_play);
    };
}, function () {
    if (!!!window.promo_play) {
        promo_play = setInterval("roll($('#J_promo'),217,500)", 4000);
    };
});
J_promo.find("a").hover(function () {
    if (!!window.promo_play) {
        promo_play = clearInterval(promo_play);
    };
}, function () {
    if (!!!window.promo_play) {
        promo_play = setInterval("roll($('#J_promo'),217,500)", 4000);
    };
});
$("#J_promo_up").hover(function () {
    if (!!window.promo_play) {
        promo_play = clearInterval(promo_play);
    };
}, function () {
    if (!!!window.promo_play) {
        promo_play = setInterval("roll($('#J_promo'),217,500)", 4000);
    };
});
$("#J_promo_down").hover(function () {
    if (!!window.promo_play) {
        promo_play = clearInterval(promo_play);
    };
}, function () {
    if (!!!window.promo_play) {
        promo_play = setInterval("roll($('#J_promo'),217,500)", 4000);
    };
});

//电影票明细 自动轮播
var J_movie = $("#J_movie");
function movieplay() {
    var li_index = J_movie.find(".tab-holder li[class='tab-current']");
    if (li_index.next("li").length < 1) {
        J_movie.find(".tab-holder li").eq(0).mouseover().mouseout();
    } else {
        li_index.next("li").mouseover().mouseout();
    };
};
var movie_play = setInterval("movieplay()", 4000);
J_movie.hover(function () {
    if (!!window.movie_play) {
        movie_play = clearInterval(movie_play);
    };
}, function () {
    if (!!!window.movie_play) {
        movie_play = setInterval("movieplay()", 4000);
    };
});
//在线客服
var snda_os_webrefer_ufg = {
    gm: 208,
    triggers: {
        floatwin: ["010101", "010102"],
        extend: [{ triggerid: "J_olservice", source: "010103" }]
    },
    t: "盛大商城-首页",
    hidewidgetwindow: true
};
$("#J_olservice").attr("href", "javascript:void(0)");
